package com.qg.wcsopcuaqj.dispatch.controller;


import com.qg.wcsopcuaqj.business.entity.TWcsTask;
import com.qg.wcsopcuaqj.business.entity.TWcsTaskLog;
import com.qg.wcsopcuaqj.business.service.ITWcsTaskLogService;
import com.qg.wcsopcuaqj.business.service.ITWcsTaskService;
import com.qg.wcsopcuaqj.dispatch.constant.QGSConstant;
import com.qg.wcsopcuaqj.dispatch.entity.RGV1Entity;
import com.qg.wcsopcuaqj.dispatch.entity.YeyaJudgeStatus;
import com.qg.wcsopcuaqj.dispatch.service.DDJService;
import com.qg.wcsopcuaqj.enums.rgv1.RGV1Plc2WcsVarEnum;
import com.qg.wcsopcuaqj.enums.rgv1.RGV1Wcs2PlcVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya1.YY1Plc2WcsVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya1.YY1Wcs2PlcVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya2.YY2Plc2WcsVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya2.YY2Wcs2PlcVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya3.YY3Plc2WcsVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya3.YY3Wcs2PlcVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya4.YY4Plc2WcsVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya4.YY4Wcs2PlcVarEnum;
import com.qg.wcsopcuaqj.opcua.controller.R;
import com.qg.wcsopcuaqj.opcua.selfunion.Enum.PLCType;
import com.qg.wcsopcuaqj.opcua.selfunion.UAService;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/12 15:15
 * @Version 1.0
 */
@RestController
@RequestMapping("/yeya")
public class WcsControllerOfYeYa {
    @Autowired
    DDJService service;

    @Autowired
    UAService uaService;

    @Autowired
    ITWcsTaskService taskService;

    @Autowired
    ITWcsTaskLogService logService;



    //todo 检查 test类 枚举类  controller类 server。impl runnable 类
    //return ok就代表满足条件 （只有等于 taskStatus ==3 才算成功），，， error就代表不满足条件
//    @PostMapping("/isReadyForYeyaStatus")
//    public R isReadyForTake(@RequestBody YeyaJudgeStatus yeyaJudgeStatus) throws Exception {
//        if(yeyaJudgeStatus.getType() == 1){
//            //入库情况
//            if(yeyaJudgeStatus.getNumber()==1){
//                //入库液压台1
//
//                Integer online = Integer.valueOf(getValue(YY1Plc2WcsVarEnum.Online));
//                Integer alarm = Integer.valueOf(getValue(YY1Plc2WcsVarEnum.Alarm));
//                Integer liftPosition = Integer.valueOf(getValue(YY1Plc2WcsVarEnum.LifterPosition));
//
//                if(online==1 && alarm==0 && liftPosition==2){
//                    return R.ok().put("taskStatus",1);
//                }else {
//                    return R.error().put("taskStatus",0);
//                }
//            }else if(yeyaJudgeStatus.getNumber()==2){
//                //入库液压台2
//                Integer taskStatus = Integer.valueOf(getValue(YY2Plc2WcsVarEnum.TaskState));
//                if(taskStatus==3){
//                    return R.ok().put("taskStatus",taskStatus);
//                }else {
//                    return R.error().put("taskStatus",taskStatus);
//                }
//            }
//        }else {
//            //出库情况
//            if(yeyaJudgeStatus.getNumber()==1){
//                //出库液压台1
//                Integer taskStatus = Integer.valueOf(getValue(YY3Plc2WcsVarEnum.TaskState));
//                if(taskStatus==3){
//                    return R.ok().put("taskStatus",taskStatus);
//                }else {
//                    return R.error().put("taskStatus",taskStatus);
//                }
//            }else if(yeyaJudgeStatus.getNumber()==2){
//                //出库液压台2
//                Integer taskStatus = Integer.valueOf(getValue(YY4Plc2WcsVarEnum.TaskState));
//                if(taskStatus==3){
//                    return R.ok().put("taskStatus",taskStatus);
//                }else {
//                    return R.error().put("taskStatus",taskStatus);
//                }
//            }
//        }
//        return R.error();
//    }
    @PostMapping("/isReadyForYeyaStatus")
    public R isReadyForTake(@RequestBody YeyaJudgeStatus yeyaJudgeStatus) throws Exception {
        if(yeyaJudgeStatus.getNumber() == 1){
            //入库液压台1
            boolean online = Boolean.parseBoolean(getValue(YY1Plc2WcsVarEnum.Online));
            boolean alarm = Boolean.parseBoolean(getValue(YY1Plc2WcsVarEnum.Alarm));
            int liftPosition = Integer.parseInt(getValue(YY1Plc2WcsVarEnum.LifterPosition));

            if(online && !alarm && liftPosition==2){
                return R.ok().put("taskStatus",1);
            }else {
                return R.error().put("taskStatus",0);
            }
        }else if(yeyaJudgeStatus.getNumber() == 2){
            boolean online = Boolean.parseBoolean(getValue(YY2Plc2WcsVarEnum.Online));
            boolean alarm = Boolean.parseBoolean(getValue(YY2Plc2WcsVarEnum.Alarm));
            int liftPosition = Integer.parseInt(getValue(YY2Plc2WcsVarEnum.LifterPosition));

            if(online && !alarm && liftPosition==2){
                return R.ok().put("taskStatus",1);
            }else {
                return R.error().put("taskStatus",0);
            }
        }else if(yeyaJudgeStatus.getNumber() == 3){
            boolean online = Boolean.parseBoolean(getValue(YY3Plc2WcsVarEnum.Online));
            boolean alarm = Boolean.parseBoolean(getValue(YY3Plc2WcsVarEnum.Alarm));
            int liftPosition = Integer.parseInt(getValue(YY3Plc2WcsVarEnum.LifterPosition));

            if(online && !alarm && liftPosition==2){
                return R.ok().put("taskStatus",1);
            }else {
                return R.error().put("taskStatus",0);
            }
        }else if(yeyaJudgeStatus.getNumber() == 4){
            boolean online = Boolean.parseBoolean(getValue(YY4Plc2WcsVarEnum.Online));
            boolean alarm = Boolean.parseBoolean(getValue(YY4Plc2WcsVarEnum.Alarm));
            int liftPosition = Integer.parseInt(getValue(YY4Plc2WcsVarEnum.LifterPosition));

            if(online && !alarm && liftPosition==2){
                return R.ok().put("taskStatus",1);
            }else {
                return R.error().put("taskStatus",0);
            }
        }
        return R.error();
    }


    private String getValue(RGV1Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private String getValue(RGV1Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private String getValue(YY1Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private String getValue(YY2Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }
    private String getValue(YY2Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private String getValue(YY3Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }
    private String getValue(YY3Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private String getValue(YY4Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }
    private String getValue(YY4Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private String getValue(YY1Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private DataValue getValues(RGV1Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }
    private DataValue getValues(RGV1Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }

    private DataValue getValues(YY1Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }
    private DataValue getValues(YY1Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }

    private DataValue getValues(YY2Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }
    private DataValue getValues(YY2Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }

    private DataValue getValues(YY3Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }
    private DataValue getValues(YY3Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }

    private DataValue getValues(YY4Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }
    private DataValue getValues(YY4Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }

    private Boolean setValue(RGV1Wcs2PlcVarEnum var, String dataType, Object newValue) throws Exception {
        return uaService.setValue(
                var.getNamespace(),
                var.getIdentity(),
                PLCType.valueOf(dataType).convertType(newValue),
                QGSConstant.plcName);
    }
    private Boolean setValue(RGV1Plc2WcsVarEnum var, String dataType, Object newValue) throws Exception {
        return uaService.setValue(
                var.getNamespace(),
                var.getIdentity(),
                PLCType.valueOf(dataType).convertType(newValue),
                QGSConstant.plcName
        );
    }
}
